package com.example.onlineeducation.ui.auth

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.View
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.FragmentActivity
import com.example.onlineeducation.BuildConfig
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseActivity
import com.example.onlineeducation.databinding.LoginActivityBinding
import com.example.onlineeducation.databinding.RegisterActivityBinding
import com.example.onlineeducation.databinding.RegisterSecondActivityBinding
import com.example.onlineeducation.ui.mine.ExamResultDetailActivity
import com.example.onlineeducation.utils.YcLoopCountDown
import com.example.onlineeducation.viewmodel.LoginVM
import com.example.onlineeducation.viewmodel.RegisterVM
import com.example.onlineeducation.widget.YcAlbumOrCameraPopupWindow
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.yc.jetpacklib.extension.showToast
import com.yc.jetpacklib.extension.ycIsEmpty
import com.yc.jetpacklib.extension.ycIsNotEmpty
import com.yc.jetpacklib.manager.YcActivityManager
import com.yc.jetpacklib.net.doSuccess
import com.yc.jetpacklib.utils.YcRegularUtil
import com.yc.jetpacklib.utils.YcRegularUtil.ycRegular

import java.util.*

/**
 *  登录
 */
class RegisterSecondActivity :
    BaseActivity<RegisterSecondActivityBinding>(RegisterSecondActivityBinding::inflate) {

    companion object {
        private const val KEY_NAME = "key_name"
        private const val KEY_PHONE = "key_phone"
        private const val KEY_ID_CARD = "key_id_card"
        private const val KEY_SEX = "key_sex"
        fun newInstance(
            context: Context,
            name: String,
            phone: String,
            idCard: String,
            sex: String,
            launcher: ActivityResultLauncher<Intent>
        ) {
            var intent = Intent(context, RegisterSecondActivity::class.java).apply {
                putExtra(KEY_NAME, name)
                putExtra(KEY_PHONE, phone)
                putExtra(KEY_ID_CARD, idCard)
                putExtra(KEY_SEX, sex)
            }
            launcher.launch(intent)
        }

        @JvmStatic
        inline fun getLauncher(
            activity: ComponentActivity,
            crossinline block: () -> Unit
        ): ActivityResultLauncher<Intent> {
            return activity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
                if (it.resultCode == RESULT_OK && it.data != null) {
                    block.invoke()
                }
            }
        }
    }

    private lateinit var mProLaunch: ActivityResultLauncher<Intent>
    private lateinit var mGroupLaunch: ActivityResultLauncher<Intent>
    private lateinit var mWorkTypeLaunch: ActivityResultLauncher<Intent>
    private val mViewModel: RegisterVM by ycViewModels()
    private var mProId: String = ""
    private var mTeamId: String = ""
    private var mWorkTypeId: String = ""
    override fun initImmersionBar() {
        ImmersionBar.with(this)
            .statusBarColor(R.color.colorPrimary)
            .statusBarDarkFont(false)
            .hideBar(BarHide.FLAG_SHOW_BAR)
            .fitsSystemWindows(true)
            .init()
    }

    override fun RegisterSecondActivityBinding.initView() {
        mProLaunch =
            RegisterSearchProjectActivity.getLauncher(this@RegisterSecondActivity) { id, name ->
                if (id != mProId) {
                    mProId = id
                    etProject.setText(name)
                    etTeam.setText("")
                    mTeamId = ""
                }
            }
        mGroupLaunch =
            RegisterSearchGroupActivity.getLauncher(this@RegisterSecondActivity) { id, name ->
                mTeamId = id
                etTeam.setText(name)
            }
        mWorkTypeLaunch =
            RegisterSearchWorkTypeActivity.getLauncher(this@RegisterSecondActivity) { id, name ->
                mWorkTypeId = id
                etWorkType.setText(name)
            }
        ivActionbarLeft.setOnClickListener {
            finish()
        }
        etProject.setParentClick {
            RegisterSearchProjectActivity.newInstance(this@RegisterSecondActivity, mProLaunch)
        }
        etTeam.setParentClick {
            if (mProId.ycIsNotEmpty()) {
                RegisterSearchGroupActivity.newInstance(
                    this@RegisterSecondActivity,
                    mProId,
                    mGroupLaunch
                )
            } else {
                showToast("请先选择项目!")
            }
        }
        etWorkType.setParentClick {
            RegisterSearchWorkTypeActivity.newInstance(this@RegisterSecondActivity, mWorkTypeLaunch)
        }
        btnRegister.setOnClickListener {
            if (mProId.ycIsEmpty()) {
                showToast("请先选择项目!")
                return@setOnClickListener
            }
            if (mTeamId.ycIsEmpty()) {
                showToast("请先选择班组!")
                return@setOnClickListener
            }
            if (mWorkTypeId.ycIsEmpty()) {
                showToast("请先选择工种!")
                return@setOnClickListener
            }
            mViewModel.register(
                intent.getStringExtra(KEY_NAME)!!,
                intent.getStringExtra(KEY_PHONE)!!,
                intent.getStringExtra(KEY_ID_CARD)!!,
                intent.getStringExtra(KEY_SEX)!!,
                mProId,
                mTeamId,
                mWorkTypeId,
                etWorkName.getText()
            )
        }
        mViewModel.mRegister.observe {
            it.doSuccess {
                val intent = Intent()
                setResult(RESULT_OK, intent)
                finish()
            }.ycShowNetError()
        }

    }


}